/**
 * 学生撤销报名
 * 
 * 置学生报名信息为空
 * 岗位的当前报名人数-1
 * 
 * 如果撤销的是自报岗位的，
 * 
 * /student/canceljob
 */




 

const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Companies = require('../../../models/companys')
const InternshipStudents = require('../../../models/internshipStudents')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager

const get_details = async function(req, res) {
    try {
        if (!req.body.year_id) {
            $.sendMessage(res, 1015, ' 参数year_id 错误')
            return
        }

        const student = await InternshipStudents.findOne({
            where: {
                student_user_name: req.user_name,
                student_year: req.body.year_id
            }
        })

        // 此学生是否存在在该年度
        if (student == null) {
            $.sendMessage(res, 1015, ' 靓仔你不属于该年度 ')
            return
        }

        // 判断是否选择了岗位
        if (student.dataValues.student_status == '0') {
            $.sendMessage(res, 1015, ' 靓仔你还没选题呢 ')
            return
        }

        // 查看学生是否被锁定
        const is_locked = await InternshipStudents.findOne({
            where: {
                student_user_name: req.user_name
            }
        })
        if (is_locked == null) {
            $.sendMessage(res, 1015, '学生不存在id')
            return
        } else if (is_locked.dataValues.is_locked == 0) {
            $.sendMessage(res, 1015, '您被锁定，不能撤销报名')
            return
        }

        // 事务
        return sequelize.transaction(function (t) {
            return InternshipStudents.update(
            {
                company_name: '',
                teacher_user_name: '',
                student_status: '0',
                teacher_user_fullname: '',
                company_id: '',
            },
            {
                where: {
                    student_user_name: req.user_name,
                    student_year: req.body.year_id
                }
            }
            , 
            {transaction: t}).then(function (user) {
            return Companies.findOne(
                {
                    where: {
                        id: student.dataValues.company_id
                    }
                }
                , {transaction: t})
            }).then( company => {
                
            return company.decrement('current_student'
            ,{transaction: t}).then( () => {
            })
            })
        })
        // 请求完整完成
        .then(function (result) {
            res.json({
                code: 200,
                message: '撤销报名成功',
                result: result
            })
            utils.create_a_log(req, '学生实习', '撤销报名')
            return
        })
        // 处理sequalize异常
        .catch(function (err) {
            res.json({
                code: 1017,
                message: '撤销报名失败，请联系管理员',
                err: 'err' + err
            })
            utils.create_a_log(req, '学生实习', '撤销报名', '失败')
            return
        })
    }
    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '浏览详情', '失败')
        return
    }
}

module.exports = get_details